import * as enumApi from "@/api/modulManage/enum";
import * as unitUpi from "@/api/modulManage/unit"
import * as attributeApi from "@/api/modulManage/attribute"
import * as attributeClassifyApi from "@/api/modulManage/attributeClassify"
import * as lifeCycleApi from "@/api/settingManage/lifeCycle";
import * as versionRulesApi from "@/api/settingManage/versionRules";
import * as codeRulesApi from "@/api/settingManage/codeRules";
import * as conditionApi from "@/api/configuration/condition";
import * as tacticsApi from "@/api/tactics/tactics";
import * as roleApi from "@/api/roleManage/roleManage";
import * as deptApi from "@/api/departmentManage/departmentManage";
import * as fileApi from "@/api/fileList/index";
import * as userGroupApi from "@/api/userGroup/userGroup";
import * as signatureTemplateManageApi from "@/api/signatureTemplateManage/index";
import * as OSSManageApi from "@/api/OSSManage/index";
import * as topMenuApi from "@/api/top_menu/index"


let apiObj = {
    "EnumManage": enumApi, //noCheckIn
    "UnitsManage": unitUpi, //noCheckIn
    "PropertiesClassify": attributeClassifyApi, //noCheckIn
    "PropertiesDefined": attributeApi, //noCheckIn
    "LifeCycle": lifeCycleApi,
    "VersionRules": versionRulesApi,
    "CodeRules": codeRulesApi,
    "Condition": conditionApi,
    "Tactics": tacticsApi,
    "roleManage": roleApi,
    "departmentManage": deptApi,
    "userGroup": userGroupApi,
    "signatureTemplateManage": signatureTemplateManageApi,
    "OSSManage": OSSManageApi,
    "fileList": fileApi,
    "topMenu": topMenuApi,
}
export default {
    data() {
        return {
            api: "",
            queryForm: {},
            page: {
                pageSize: 10,
                currentPage: 1,
                total: 0
            },
            data: [],
            loading: false,
            selection: [],

            visible: false,
            tabsList: [],

            ifIdKey: ["LifeCycle", "CodeRules"]
        }
    },
    computed: {
        idKey() {
            if (["VersionRules", "CodeRules"].includes(this.$options.name)) {
                return "masterVersionId"
            }
            if (["roleManage"].includes(this.$options.name)) {
                return "pid"
            }
            return this.ifIdKey.includes(this.$options.name) ? "id" : "oid";
        },
        id() {
            let idKey = "oid";
            if (["LifeCycle"].includes(this.$options.name)) {
                idKey = "id"
            }
            return idKey;
        }
    },
    methods: {
        //检入
        checkIn(row) {
            this.$confirm("是否检入").then(() => {
                this.api.publish({id: row[this.id]}).then(() => {
                    this.$message.success("检入成功");
                    this.getList();
                })
            })
        },
        // 撤销检出
        unCheckOut(row) {
            this.$confirm("是否撤销检出").then(() => {
                this.api.unCheckOut({id: row[this.id]}).then(() => {
                    this.$message.success("撤销成功");
                    this.getList();
                })
            })
        },
        // 向抽屉中添加tab页 根据 oid 去重
        addToTabsList(title, row, component, type) {
            let ids = this.tabsList.map(item => item.row[this.idKey]);
            if (!ids.includes(row[this.idKey])) {
                this.tabsList.push({title, row, component, type})
            }
            this.visible = true;
        },
        // 删除
        remove(data) {
            this.$confirm("是否删除？").then(() => {
                let ids = data.map(item => item[this.idKey]);
                this.api.del({ids: ids.join(",")}).then(() => {
                    this.$message.success("删除成功");
                    this.getList();
                    this.selection = [];
                })
            })
        },
        // 启用/停用
        start_stop(data, type) {
            let msg = type === "start" ? "启用成功" : "停用成功";
            let ids = data.map(item => item[this.idKey]);
            this.api[type]({ids: ids.join(',')}).then(() => {
                this.$message.success(msg);
                this.getList();
            })
        },
        getList() {
            this.loading = true;
            this.api.getList({
                current: this.page.currentPage,
                size: this.page.pageSize, ...this.queryForm
            }).then(res => {
                let list = [];
                let totalRows = 0;
                if (this.notPage) {
                    list = res.data.data;
                } else {
                    list = res.data.data.list;
                    totalRows = res.data.data.totalRows;
                }

                this.page.total = totalRows;
                this.data = list;
                this.loading = false;
            })
        },
        currentChange(currentPage) {
            this.page.currentPage = currentPage;
            this.getList();

        },
        sizeChange(pageSize) {
            this.page.pageSize = pageSize;
            this.getList();
        },
    },
    mounted() {
        this.api = apiObj[this.$options.name];
        this.getList();
        this.$nextTick(() => {      // 表格错位解决方法
            this.$refs.crud && this.$refs.crud.refreshTable();
        })
    }
}
